#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :examcoo_view.py
# @Time      :2023/12/5 
# @Author    :CL
# @email     :1037654919@qq.com
import random
import re
import requests
from retrying import retry
from utils import mongo_manager,get_proxy
from multiprocessing import Pool

examcoo_index = mongo_manager('examcoo_index', db='public_data')
examcoo_paper = mongo_manager('examcoo_paper', db='public_data')

# pigcha
proxies = {'http': '127.0.0.1:15732',
           'https': '127.0.0.1:15732'}
ua_list = [
    "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36",#Chrome
    "Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0",#firwfox
    "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",#IE
    "Opera/9.99 (Windows NT 5.1; U; zh-CN) Presto/9.9.9",#Opera
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0"
]
headers = {
    "User-Agent":random.choice(ua_list),
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    # "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Pragma": "no-cache",
    "Cache-Control": "no-cache"
}
cookies = {
    "PHPSESSID": "1l3niapfmmh22h1hgtm9npm3h2"
}


@retry(stop_max_attempt_number=5)
def get_vp4tokenpid(url="https://www.examcoo.com/editor/do/view/id/1115083"):
    response = requests.get(url, headers=headers, cookies=cookies, timeout=5,proxies=get_proxy())
    # print(response.text)
    print(response.url, response)
    pattern = r'vp4tokenpid\s*=\s*"([^"]+)"'
    match = re.search(pattern, str(response.text))
    # 输出提取的 vp4tokenpid 值
    if match:
        # print(match.group(1))
        return match.group(1)


@retry(stop_max_attempt_number=5)
def get_view(paperid='1115083', vp4tokenpid='38a5054bb32994c093e36683b8c0bfe2'):
    # headers["Referer"] = f"https://www.examcoo.com/editor/do/view/id/{paperid}"
    # cookies = {
    #     "PHPSESSID": "2ah85sgmt8m48rde5h36ch3c14"
    # }
    url = f"https://www.examcoo.com/editor/rpc/getpapercontent/pid/{paperid}/tokenpid/{vp4tokenpid}/fromAction/view"
    response = requests.get(url, proxies=get_proxy(),timeout=5)

    # print(response.text)
    print(response.url, response)
    return response.json()

def get_info(seed):
    # print(seed['_id'])
    vp4tokenpid = get_vp4tokenpid(url=seed['href'])
    print(vp4tokenpid)
    seed['vp4tokenpid'] = vp4tokenpid
    view = get_view(paperid=seed['试卷编号'], vp4tokenpid=vp4tokenpid)
    seed['view'] = view
    examcoo_paper.updateOne({'_id': seed['_id']}, seed)
def main():
    print()
    seeds = examcoo_paper.findAll({'vp4tokenpid': None})
    work_list=[]
    for data in seeds:
        work_list.append(data)
    print(len(work_list))
    pool = Pool(processes=10)
    pool.map(get_info, work_list)
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()


if __name__ == "__main__":
    print('begin')
    # get_vp4tokenpid()
    # get_view()
    main()

    examcoo_index.close()
    examcoo_paper.close()
